<template>
	<el-main class="bg edit_wrap">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
			<el-col v-if="user_group === '管理员' || $check_field('get','manager') || $check_field('add','manager') || $check_field('set','manager')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="经理" prop="manager">
						<el-select v-if="user_group === '管理员' || (form['public_documents_id'] && $check_field('set','manager')) || (!form['public_documents_id'] && $check_field('add','manager'))" id="manager" v-model="form['manager']" :disabled="disabledObj['manager_isDisabled']">
							<el-option v-for="o in list_user_manager" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
						<el-select v-else-if="$check_field('get','manager')" id="manager" v-model="form['manager']" :disabled="true">
							<el-option v-for="o in list_user_manager" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','file_name') || $check_field('add','file_name') || $check_field('set','file_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="文件名称" prop="file_name">
					<el-input id="file_name" v-model="form['file_name']" placeholder="请输入文件名称"
							  v-if="user_group === '管理员' || (form['public_documents_id'] && $check_field('set','file_name')) || (!form['public_documents_id'] && $check_field('add','file_name'))" :disabled="disabledObj['file_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','file_name')">{{form['file_name']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','file_type') || $check_field('add','file_type') || $check_field('set','file_type')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="文件类型" prop="file_type">
					<el-input id="file_type" v-model="form['file_type']" placeholder="请输入文件类型"
							  v-if="user_group === '管理员' || (form['public_documents_id'] && $check_field('set','file_type')) || (!form['public_documents_id'] && $check_field('add','file_type'))" :disabled="disabledObj['file_type_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','file_type')">{{form['file_type']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','document_attachment') || $check_field('add','document_attachment') || $check_field('set','document_attachment')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="文件附件" prop="document_attachment">
					<div v-if="disabledObj['document_attachment_isDisabled']">
						<div v-if="$check_field('get','document_attachment')">
							<el-button type="primary" @click="$download($fullUrl(form['document_attachment']),form['document_attachment'])">下载<i
									class="el-icon-download el-icon--right"></i></el-button>
						</div>
					</div>
					<div v-else>
						<el-upload v-if="user_group === '管理员' || (form['public_documents_id'] && $check_field('set','document_attachment')) || (!form['public_documents_id'] && $check_field('add','document_attachment'))" class="upload-demo" drag
								   action="" style="max-width: 300px;width: 100%;" :http-request="upload_document_attachment" :limit="1" accept="">
							<i class="el-icon-upload"></i>
							<div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
						</el-upload>
						<div v-else-if="$check_field('get','document_attachment')">
							<el-button type="primary" @click="$download($fullUrl(form['document_attachment']),form['document_attachment'])">下载<i
									class="el-icon-download el-icon--right"></i></el-button>
						</div>
					</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','document_details') || $check_field('add','document_details') || $check_field('set','document_details')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="文件详情" prop="document_details">
					<el-input type="textarea" id="document_details" v-model="form['document_details']" placeholder="请输入文件详情"
						v-if="user_group === '管理员' || (form['public_documents_id'] && $check_field('set','document_details')) || (!form['public_documents_id'] && $check_field('add','document_details'))" :disabled="disabledObj['document_details_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','document_details')">{{form['document_details']}}</div>
				</el-form-item>
			</el-col>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item>
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";

	export default {
		mixins: [mixin],
		data() {
			return {
				field: "public_documents_id",
				url_add: "~/api/public_documents/add?",
				url_set: "~/api/public_documents/set?",
				url_get_obj: "~/api/public_documents/get_obj?",
				url_upload: "~/api/public_documents/upload?",

				query: {
					"public_documents_id": 0,
				},

				form: {
					"manager": 0, // 经理
					"file_name":'', // 文件名称
					"file_type":'', // 文件类型
					"document_attachment":'', // 文件附件
					"document_details":'', // 文件详情
					"public_documents_id": 0, // ID

				},
				disabledObj:{
					"manager_isDisabled": false,
					"file_name_isDisabled": false,
					"file_type_isDisabled": false,
					"document_attachment_isDisabled": false,
					"document_details_isDisabled": false,
				},
				// 用户列表
				list_user_manager: [],
			}
		},
		methods: {
			/**
			 * 获取经理用户用户列表
			 */
			async get_list_user_manager() {
                // if(this.user_group !== "管理员" && this.form["manager"] === 0) {
                //     this.form["manager"] = this.user.user_id;
                // }
                var json = await this.$get("~/api/user/get_list?user_group=经理用户");
                if(json.result && json.result.list){
                    this.list_user_manager = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
			get_user_manager(id){
				var obj = this.list_user_manager.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			/**
			 * 上传文件附件
			 * @param {Object} param文件参数
			 */
			upload_document_attachment(param){
				this.uploadFile(param.file, "document_attachment");
			},

			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
				// 获取缓存数据附加
				form = $.db.get("form");
				$.push(this.form ,form);
				if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
							}
						})
					})
				}
				$.db.del("form");
				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){
			},

			is_view(){
				var bl = this.user_group == "管理员";

				if(!bl){
					bl = this.$check_action('/public_documents/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/public_documents/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/public_documents/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/public_documents/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/public_documents/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
			this.get_list_user_manager();
		},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
